package main

import (
	"fmt"
	"time"
)

func main() {

	var ch = make(chan string)

	go func() {
		defer fmt.Println("子协程1结束")
		for i := 0; i < 2; i++ {
			fmt.Println("子协程1 i = ", i)
			time.Sleep(time.Second)
		}
		ch <- "我是子协程1，工作完毕"
		//defer func() {ch <- "我是子协程，工作完毕"}()
	}()

	go func() {
		defer fmt.Println("子协程2结束")
		for i := 0; i < 2; i++ {
			fmt.Println("子协程2 i = ", i)
			time.Sleep(time.Second)
		}
		ch <- "我是子协程2，工作完毕"
		//defer func() {ch <- "我是子协程，工作完毕"}()
	}()

	i := <-ch
	fmt.Println("接收到消息：", i)

	i = <-ch

	fmt.Println("接收到消息：", i)

	defer fmt.Println("主协程结束")

}
